export default {
  // 为当前模块开启命名空间
  namespaced: true,

  // 模块的 state 数据
  state: () => ({
    item: 0,
    audioCtx: {},
    // 歌曲状态属性
    audioState: 'paused',
    waitFlag: false,
    playIndex: parseInt(uni.getStorageSync('playIndex') || -1),
    sliderValue: 0,
    // 推荐歌单的数据列表
    musicLists: [],
    // 用户的歌曲列表
    musicMyList: JSON.parse(uni.getStorageSync('musicMyList') || '[]'),
    // 最近播放歌曲列表
    recentPlayList: JSON.parse(uni.getStorageSync('recentPlayList') || '[]'),
    // 播放列表
    playList: JSON.parse(uni.getStorageSync('playList') || '[]'),
    // 正在播放的歌曲信息
    playInfo: {
      currentTime: '00:00',
      duration: '00:00',
      percent: 0,
      title: '',
      singer: '',
      imgUrl: '',
    },
    // 歌单列表标志
    songSheetList: null,
    // 歌曲爱心图标标志
    islike: false,
  }),

  // 模块的 mutations 方法
  mutations: {
    updateItem(state, item) {
      state.item = item
    },
    setAudioCtx(state) {
      if (state.audioState === 'paused' && state.playInfo.currentTime === '00:00' && state.playInfo.duration ===
        '00:00') {
        state.audioCtx = uni.createInnerAudioContext();
      }
    },
    setState(state, audioState) {
      state.audioState = audioState
    },
    setToMusicList(state, musicLists) {
      state.musicLists = musicLists
    },
    //进入页面设置播放信息
    setPlayInfo(state, savePlayInfo) {
      state.audioCtx.src = savePlayInfo.musicSrc
      state.playInfo.title = savePlayInfo.title
      state.playInfo.singer = savePlayInfo.singer
      state.playInfo.imgUrl = savePlayInfo.imgUrl
    },
    // 更新播放列表数据
    updateToPlayList(state, musicItem) {
      var insertIndex = state.playIndex + 1;
      const findResultIndex = state.playList.findIndex((x) => x.title === musicItem.title);
      if (findResultIndex !== -1) { // 如果找到了相同的音乐项
        state.playList.splice(findResultIndex, 1); // 先删除相同的项
        if (findResultIndex < insertIndex) {
          insertIndex-- // 如果删除的项在插入位置前面，需要将插入位置减1
          state.playIndex--
        }
      }
      state.playList.splice(insertIndex, 0, musicItem); // 在指定位置插入 musicItem
      this.commit('m_music/saveToStorage', 3)
      this.commit('m_music/saveToStorage', 4)
    },
    // 删除播放列表数据
    deletePlayList(state, musicItem) {
      const index = state.playList.indexOf(musicItem)
      if (index !== -1) {
        state.playList.splice(index, 1)
      }
      // 更新playIndex的值
      if (index < state.playIndex) {
        state.playIndex = state.playIndex - 1
      }
      this.commit('m_music/saveToStorage', 3)
      this.commit('m_music/saveToStorage', 4)
    },
    // 底部播放器默认选择第 1 首
    setMusic(state, index) {
      var music = state.playList[index]
      if (!music) {
        return
      }
      state.audioCtx.src = music.musicSrc
      state.playIndex = index
      state.playInfo.title = music.title
      state.playInfo.singer = music.singer
      state.playInfo.imgUrl = music.imgUrl
      this.commit('m_music/saveToStorage', 4)

      // 添加播放歌曲到最近播放列表中
      const findResultIndex = state.recentPlayList.findIndex((item) => item.title === music.title);
      if (findResultIndex !== -1) {
        state.recentPlayList.splice(findResultIndex, 1); // 删除原有数据
      }
      state.recentPlayList.unshift(music); // 添加新数据到播放列表的第一个位置
      this.commit('m_music/saveToStorage', 2)

      //判断正在播放的歌曲是否存在于我喜欢的音乐列表中
      const findResult = state.musicMyList.find((x) => x.title === music.title)
      if (!findResult) {
        state.islike = false
      } else {
        state.islike = true
      }
    },
    audioWait(state) {
      state.audioCtx.onWaiting(() => {
        state.audioCtx.pause() // 等待网络的时候音频暂停
        state.waitFlag = true // 标明是onWaiting触发的暂停
      })
      // 音频准备就绪的回调
      state.audioCtx.onCanplay(() => {
        if (state.waitFlag) { // 如果是onWaiting触发的暂停，就立即播放
          state.audioCtx.play() // play()重新触发onTimeUpdata()回调
          state.waitFlag = false // 取消相应的flag标志位
        }
      })
    },
    // 更新歌单列表标志
    updateSongSheetList(state, indexList) {
      state.songSheetList = indexList
    },
    // 设置我喜欢的音乐列表
    setUserMusicList(state, musicList) {
      state.musicMyList = musicList
      this.commit('m_music/saveToStorage', 1)
    },
    // 更新我的歌单事件
    updateUserMusicList(state, musicList) {
      state.musicMyList = musicList
      //判断正在播放的歌曲是否存在于我喜欢的音乐列表中
      const findResult = state.musicMyList.find((x) => x.title === state.playInfo.title)
      if (!findResult) {
        state.islike = false
      } else {
        state.islike = true
      }
      this.commit('m_music/saveToStorage', 1)
    },

    // 添加歌曲到播放列表中
    addToPlayList(state, musicMyList) {
      musicMyList.forEach((musicItem) => {
        const findResultIndex = state.playList.findIndex((x) => x.title === musicItem.title);

        if (findResultIndex !== -1) { // 如果找到了相同的音乐项
          if (findResultIndex >= state.playIndex) {
            return
          }
          state.playList.splice(findResultIndex, 1); // 先删除相同的项
          if (findResultIndex < state.playIndex) {
            state.playIndex--
          }
        }
        state.playList.push(musicItem);
      });
      this.commit('m_music/saveToStorage', 3)
      this.commit('m_music/saveToStorage', 4)
    },
    // 清空我的歌单数据事件
    emptyMusicMyList(state) {
      var musicMyList = []
      state.musicMyList = musicMyList
      state.islike = false
      this.commit('m_music/saveToStorage', 1)
    },
    // 清空最近播放歌单事件
    emptyRecentPlayList(state) {
      var recentPlayList = []
      state.recentPlayList = recentPlayList
      this.commit('m_music/saveToStorage', 2)
    },
    // 将数据持久化存储到本地
    saveToStorage(state, index) {
      if (index === 1) {
        uni.setStorageSync('musicMyList', JSON.stringify(state.musicMyList))
      } else if (index === 2) {
        uni.setStorageSync('recentPlayList', JSON.stringify(state.recentPlayList))
      } else if (index === 3) {
        uni.setStorageSync('playList', JSON.stringify(state.playList))
      } else if (index === 4) {
        uni.setStorageSync('playIndex', JSON.stringify(state.playIndex))
      }
    },
  },
  // 模块的 getters 属性
  getters: {},
}